//
// Created by Administrator on 2024/11/1.
//

#include "JsonResponse.h"

void JsonResponse::success(struct mg_connection *conn) const {
    json json_response;
    json_response["msg"] = msg;
    json_response["data"] = data;
    json_response["code"] = code;

    std::string response_string = json_response.dump(0);
    mg_send_http_ok(conn, "application/json; charset=utf-8",  static_cast<long long>(response_string.size()));
    mg_write(conn, response_string.c_str(), response_string.size());
}

void JsonResponse::error(struct mg_connection *conn) const {
    json json_response;
    json_response["msg"] = msg;
    json_response["data"] = json::object();
    json_response["code"] = 500;
    std::string response_string = json_response.dump(0);
    mg_send_http_ok(conn, "application/json; charset=utf-8", static_cast<long long>(response_string.size()));
    mg_write(conn, response_string.c_str(), response_string.size());
}




